package com.google.project.client.Operator;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.DailyReportService;
import com.google.project.client.Service.DailyReportServiceAsync;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Service.MessageService;
import com.google.project.client.Service.MessageServiceAsync;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.DailyReportDTO;
import com.google.project.shared.MessageDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.UsersDTO;

public class OPTHomePage extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField
	Hyperlink hypMessNew;
	@UiField
	Hyperlink hypReportNew;
	@UiField Label lblTitle;
	@UiField Label lblTaskInfo;
	@UiField Hyperlink hypNumOfTask;
	@UiField Hyperlink hypcurrTask;
	@UiField Hyperlink hypNewTask;
	@UiField Label lblMess;
	@UiField Label lblDailyReport;
	@UiField Hyperlink hypFinishTask;
	@UiField Hyperlink hypCancel;
	

	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);
	private final MessageServiceAsync messageService = GWT
			.create(MessageService.class);
	private final DailyReportServiceAsync dailyReportService = GWT
	.create(DailyReportService.class);
	private final ProjectServiceAsync projectService = GWT.create(ProjectService.class);
	private List<DailyReportDTO> listDailyReportDTO = new ArrayList<DailyReportDTO>();
	List<MessageDTO> listMessage = new ArrayList<MessageDTO>();
	private List<ProjectTaskDTO> listTask = new ArrayList<ProjectTaskDTO>();
	UsersDTO userDTO = new UsersDTO();
	MessageDTO messDTO = new MessageDTO();
	
	interface Binder extends UiBinder<Widget, OPTHomePage> {
	}

	public OPTHomePage() {
		initWidget(binder.createAndBindUi(this));
		getNewReportByUserId(); //lấy tất cả report mới
		getAllMess();

		
	}

	// get info user login from session
	public void getNewReportByUserId() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(UsersDTO result) {
				userDTO = result;
				getDailyReportByProject(2);
				getListTask();
			}
		});
	}
	// get all Message
	public void getAllMess() {
		messageService.getMessageList(new AsyncCallback<List<MessageDTO>>() {

			@Override
			public void onSuccess(List<MessageDTO> result) {
				for (MessageDTO messageDTO : result) {
					listMessage.add(messageDTO);
				}
				LoadMess();
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	// get Message where (userId=receiveId and status=1) or (userId=senderId and
	// status=2) & parentID=0
	private void LoadMess() {
		Boolean condition = true;
		List<MessageDTO> temple = new ArrayList<MessageDTO>();

		for (MessageDTO mess : listMessage) {
			condition = condition
					&& (mess.getTypeId() == 1)
					&& (mess.getParentId().equalsIgnoreCase("0") && (((mess
							.getStatus() == 1)
							&& mess.getReceiverId().equals(userDTO.getUserId()) || ((mess
							.getStatus() == 2) && mess.getSenderId().equals(
							userDTO.getUserId())))));
			if (condition) {
				temple.add(mess);
			}
			condition = true;
		}
		lblTitle.setText("THÔNG TIN TỔNG HỢP");
		lblMess.setText("Các vấn đề cần giải quyết :");
		lblDailyReport.setText("Báo cáo  ngày :");
		lblTaskInfo.setText("Công việc được giao:");
		//đếm tin nhắn mới
		if (temple != null) {
			hypMessNew.setText("Tin nhắn chưa đọc: " + temple.size() + " (tin nhắn)");
		}else{
			hypMessNew.setText("Tin nhắn chưa đọc: 0 (tin nhắn)");
		}
	}
	// lấy về báo cáo ngày theo ProjectID, status
	public void getDailyReportByProject(final int statusReport) {
		// listDailyReportDTO=new
		dailyReportService.getDailyReportByProjectId(userDTO.getCurrentProjID(),
				new AsyncCallback<List<DailyReportDTO>>() {

					@Override
					public void onSuccess(List<DailyReportDTO> result) {
						Boolean condition = true;
						for (DailyReportDTO dailyReportDTO : result) {
							condition = condition
							&& (dailyReportDTO.getStatus() == statusReport);
							
							if (condition
									&& dailyReportDTO.getFromUserID().equals(
											userDTO.getUserId())) {
								listDailyReportDTO.add(dailyReportDTO);
							}
							condition = true;
						}

						//if (listDailyReportDTO != null) {
							hypReportNew.setText("Phản hồi chưa đọc: " + listDailyReportDTO.size() + " (phản hồi)");
						//}
					}

					@Override
					public void onFailure(Throwable caught) {
						// Window.alert("fail");

					}
				});
	}

	//Lấy về tổng số task được giao
	public void getListTask(){
	projectService.getListTask(userDTO.getCurrentProjID(), new AsyncCallback<List<ProjectTaskDTO>>() {
		
		@Override
		public void onSuccess(List<ProjectTaskDTO> result) {
			for(ProjectTaskDTO projectTaskDTO :result){
				if(projectTaskDTO.getGroup()==0){
					listTask.add(projectTaskDTO);
				}
			}
			   int newTask = 0;
			   int currTask =0;
			   int finishTask =0;
			   int cancel =0;
			List<ProjectTaskDTO> listData = new ArrayList<ProjectTaskDTO>();
			for(ProjectTaskDTO task :listTask)
			{
				if(task.getOptUserId().equals(userDTO.getUserId()))
				{
					listData.add(task);
					if(task.getStatus()==1)
					{
						
						currTask++;
					}
					if(task.getStatus()==0)
					{
						newTask++;
					}
					if(task.getStatus()==2)
					{
						finishTask++;
					}
					if(task.getStatus()==3)
					{
						cancel++;
					}
				}
					
				//}
			}
			hypNumOfTask.setText("Tổng số công việc: "+ listData.size() + " (Công việc)");
			hypcurrTask.setText("- Đang thực hiện: " + currTask + " (Công việc)");
			hypNewTask.setText("- Chưa thực hiện: " + newTask + " (Công việc)");
		    hypFinishTask.setText("- Đã hoàn thành: " + finishTask + " (Công việc)");
			hypCancel.setText("- Ngừng thực hiện: "  + cancel + " (Công việc)");
			//hypcurrTask.setText("Đang thực hiện : "+currTask + "  | Chưa thực hiện : "+ newTask +" |  Hoàn thành : "+finishTask+" | Ngừng thực hiện :" +cancel);
		}
		
		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub
			Window.alert("Có lỗi trong quá trình Tải thông tin!");
		}
	});
	
}
	//tin nhắn mới
	@UiHandler("hypMessNew")
	void onHypMessNewClick(ClickEvent event) {
		OPTEntrypoint.get().OPTMessageList();
	}
	@UiHandler("hypReportNew")
	void onHypReportNewClick(ClickEvent event) {
		OPTEntrypoint.get().OPTDailyReportList();
	}
	@UiHandler("hypNumOfTask")
	void onHypNumOfTaskClick(ClickEvent event) {
		OPTEntrypoint.get().OPTProjectDetail(userDTO.getCurrentProjID(),-1);
	}
	@UiHandler("hypcurrTask")
	void onHypcurrTaskClick(ClickEvent event) {
		OPTEntrypoint.get().OPTProjectDetail(userDTO.getCurrentProjID(),1);
	}
	@UiHandler("hypNewTask")
	void onHypNewTaskClick(ClickEvent event) {
		OPTEntrypoint.get().OPTProjectDetail(userDTO.getCurrentProjID(),0);
	}
	@UiHandler("hypFinishTask")
	void onHypFinishTaskClick(ClickEvent event) {
		OPTEntrypoint.get().OPTProjectDetail(userDTO.getCurrentProjID(),2);
	}
	@UiHandler("hypCancel")
	void onHypCancelClick(ClickEvent event) {
		OPTEntrypoint.get().OPTProjectDetail(userDTO.getCurrentProjID(),3);
	}
}
